package com.pro.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.dao.DianzanStatusMapper;
import com.pro.dao.EvaluationMapper;
import com.pro.domain.DianzanStatus;
import com.pro.domain.Evaluation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DianzanStatusServiceImpl implements DianzanStatusService {
    @Autowired
    private DianzanStatusMapper dianzanStatusMapper;
    @Autowired
    private EvaluationMapper evaluationMapper;

    @Override
    public void changeDianzanStatus(Long evaluationId, Long memberId) {
        QueryWrapper<DianzanStatus> queryWrapper = new QueryWrapper<DianzanStatus>();
        queryWrapper.eq("evaluation_id",evaluationId);
        queryWrapper.eq("member_id",memberId);
        DianzanStatus dianzanStatus = dianzanStatusMapper.selectOne(queryWrapper);

        Evaluation evaluation = evaluationMapper.selectById(evaluationId);

        if(dianzanStatus==null){
            evaluation.setEnjoy(evaluation.getEnjoy()+1);
            evaluationMapper.updateById(evaluation);
            dianzanStatus = new DianzanStatus();
            dianzanStatus.setStatus(1);
            dianzanStatus.setEvaluationId(evaluationId);
            dianzanStatus.setMemberId(memberId);
            dianzanStatusMapper.insert(dianzanStatus);
        }else if(dianzanStatus.getStatus()==1){
            evaluation.setEnjoy(evaluation.getEnjoy()-1);
            evaluationMapper.updateById(evaluation);
            dianzanStatus.setStatus(0);
            dianzanStatusMapper.updateById(dianzanStatus);
        }else if(dianzanStatus.getStatus()==0){
            evaluation.setEnjoy(evaluation.getEnjoy()+1);
            evaluationMapper.updateById(evaluation);
            dianzanStatus.setStatus(1);
            dianzanStatusMapper.updateById(dianzanStatus);
        }

    }
}
